<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({ status: '9' })" v-auth="'msg:msgNotify:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-button type="primary" @click="handlePushed('0')" v-auth="'msg:msgNotify:edit'">
          <eye-outlined /> {{ t('发布') }}
        </a-button>
        <a-button type="primary" @click="handlePushed('9')" v-auth="'msg:msgNotify:edit'">
          <eye-invisible-outlined /> {{ t('取消发布') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id, status: record.status })">
          {{ record.msgTitle }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <ViewForm @register="registerViewDrawer" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMsgMsgNotifyList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { msgNotifyDelete, msgNotifyListData, updateStatus } from '/@/api/msg/msgNotify';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import ViewForm from './view.vue';

  const { t } = useI18n('msg.msgNotify');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('公告管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('标题'),
        field: 'msgTitle',
        component: 'Input',
      },
      {
        label: t('发送人'),
        field: 'sendUserName',
        component: 'Input',
      },
      {
        label: t('车间'),
        field: 'workShop',
        component: 'Select',
        componentProps: { dictType: 'srm_work_shop', allowClear: true, onChange: handleSuccess },
      },
      {
        label: t('公告类型'),
        field: 'notifyType',
        component: 'Select',
        componentProps: {
          dictType: 'msg_inner_notify_type',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('发送时间'),
        field: 'sendDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'msg_inner_msg_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('发送时间'),
      dataIndex: 'sendDate',
      key: 'a.send_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('标题'),
      dataIndex: 'msgTitle',
      key: 'a.msg_title',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('公告类型'),
      dataIndex: 'notifyType',
      key: 'a.notify_type',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'msg_inner_notify_type',
    },
    {
      title: t('车间'),
      dataIndex: 'workShop',
      key: 'a.work_shop',
      sorter: true,
      width: 130,
      align: 'left',
      dictType: 'srm_work_shop',
    },
    {
      title: t('发送人'),
      dataIndex: 'sendUserName',
      key: 'a.send_user_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('开始时间'),
      dataIndex: 'startDate',
      key: 'a.start_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('结束时间'),
      dataIndex: 'endDate',
      key: 'a.end_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'msg_inner_msg_status',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'ant-design:file-text-outlined',
        title: t('查看公告'),
        onClick: handleForm.bind(this, { id: record.id, status: record.status }),
        auth: 'msg:msgNotify:view',
        ifShow: () => record.status !== '9',
        enable: true,
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑公告'),
        onClick: handleForm.bind(this, { id: record.id, status: record.status }),
        auth: 'msg:msgNotify:edit',
        ifShow: () => record.status === '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除公告'),
        popConfirm: {
          title: t('是否确认删除公告'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'msg:msgNotify:edit',
        ifShow: () => record.status === '9',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerViewDrawer, { openDrawer: openViewDrawer }] = useDrawer();
  const [registerTable, { reload, getSelectRowKeys }] = useTable({
    api: msgNotifyListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    if (record.status !== '9') {
      openViewDrawer(true, record);
      return;
    }
    openDrawer(true, record);
  }

  function handlePushed(status: string) {
    let selIds = getSelectRowKeys();
    if (selIds.length <= 0) {
      showMessage('请至少选择一条要打印的数据!');
      return;
    }
    let params = {
      selIds: selIds.join(','),
      status: status,
    };
    updateStatus(params).then((res) => {
      showMessage(res.message);
      handleSuccess();
    });
  }

  async function handleDelete(record: Recordable) {
    const res = await msgNotifyDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  function extractTextFromHtml(html) {
    // 创建一个临时的div元素
    const tempDiv = document.createElement('div');

    // 设置该元素的innerHTML为传入的html字符串
    tempDiv.innerHTML = html;

    // 使用textContent属性获取纯文本
    const text = tempDiv.textContent || tempDiv.innerText;

    return text;
  }
</script>
